//
//  CustomTabBarController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/6.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class CustomTabBarController: UITabBarController,TabBarDelegate {
    
    
    weak var customTabBar = TabBar()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.white
        
        setUptabbar()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        for child in self.tabBar.subviews {
            if child.isKind(of: UIControl.self) {
                child.removeFromSuperview()
            }
        }
    }
}

// MARK:- 设置tabbar
extension CustomTabBarController {
    ///设置tabbar
    func setUptabbar() -> Void {
        let customTabBar = TabBar.init(frame: self.tabBar.bounds)
        customTabBar.TabbarDelegate = self
        self.tabBar.addSubview(customTabBar)
        self.customTabBar = customTabBar
    }
    
    func setupChildVC(_ childVC: UIViewController,title: String,imageName: String,selectImageName: String, isAnimation: TabbarHideStyle){
        
        childVC.title = title
        childVC.tabBarItem.image = UIImage.init(named: imageName)
        //        不在渲染图片
        childVC.tabBarItem.selectedImage = UIImage.init(named: selectImageName)?.withRenderingMode(.alwaysOriginal)
        
//        let navigationCtrl = RZNavigationController.init(rootViewController: childVC)
//        navigationCtrl.tabbarHideStyle = isAnimation
//        self.addChildViewController(navigationCtrl)
        self.addChildViewController(childVC)
        //        添加tabbar内部按钮
        self.customTabBar!.addTabbarButtonWith(childVC.tabBarItem)
        
    }

//    func setupChildVC(_ childVC: UIViewController,imageName: String,selectImageName: String, isAnimation: TabbarHideStyle){
//        
////        childVC.title = title
//        childVC.tabBarItem.image = UIImage.init(named: imageName)
//        //        不在渲染图片
//        childVC.tabBarItem.selectedImage = UIImage.init(named: selectImageName)?.withRenderingMode(.alwaysOriginal)
//        
//        //        let navigationCtrl = RZNavigationController.init(rootViewController: childVC)
//        //        navigationCtrl.tabbarHideStyle = isAnimation
//        //        self.addChildViewController(navigationCtrl)
//        self.addChildViewController(childVC)
//        //        添加tabbar内部按钮
//        self.customTabBar!.addTabbarButtonWith(childVC.tabBarItem)
//        
//    }
    
    func tabbar(tabbar: TabBar, formWhichItem: Int, tpWhichItem: Int) {
        self.selectedIndex = tpWhichItem
    }
    
    
}
